package com.sls.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sls.mall.product.entity.BrandEntity;
import com.sls.mall.product.entity.CategoryEntity;
import com.sls.mall.product.service.AttrAttrgroupRelationService;
import com.sls.mall.product.service.BrandService;
import com.sls.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sls.mall.common.utils.PageUtils;
import com.sls.mall.common.utils.Query;

import com.sls.mall.product.dao.CategoryBrandRelationDao;
import com.sls.mall.product.entity.CategoryBrandRelationEntity;
import com.sls.mall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    // 如果是调用其他的业务逻辑 最好调用 Service  应为 Service 具有可扩展性
    @Autowired
    BrandService brandService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    CategoryBrandRelationDao categoryBrandRelationDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDeatil(CategoryBrandRelationEntity categoryBrandRelationEntity) {

        BrandEntity brandEntity = brandService.getById(categoryBrandRelationEntity.getBrandId());

        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelationEntity.getCatelogId());

        categoryBrandRelationEntity.setBrandName(brandEntity.getName());

        categoryBrandRelationEntity.setCatelogName(categoryEntity.getName());

        save(categoryBrandRelationEntity);

    }

    @Override
    public void updateBrand(Long brandId, String name) {

        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);

        update(categoryBrandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    @Override
    public void updateCategory(Long catId, String name) {

//        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
//        categoryBrandRelationEntity.setCatelogId(catId);
//        categoryBrandRelationEntity.setCatelogName(name);
//
//        update(categoryBrandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));

        this.baseMapper.updateCategory(catId,name);

    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {

        List<CategoryBrandRelationEntity> catelog_id = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<BrandEntity> collect = catelog_id.stream().map(item -> {
            Long brandId = item.getBrandId();

            BrandEntity brandEntity = brandService.getById(brandId);
            return brandEntity;
        }).collect(Collectors.toList());

        return collect;

    }
}